<html>
<head><meta charset="utf-8"><title>PSA: always assert never · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html">PSA: always assert never</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222732623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222732623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222732623">(Jan 14 2021 at 15:28)</a>:</h4>
<p>Hey, I suggest changing the strategy of asserts in rust-analyzer: <a href="https://github.com/rust-analyzer/rust-analyzer/pull/7270">https://github.com/rust-analyzer/rust-analyzer/pull/7270</a></p>



<a name="222732696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222732696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222732696">(Jan 14 2021 at 15:29)</a>:</h4>
<p>TL;DR: instead of <code>assert!(cond)</code>, we now write</p>



<a name="222732700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222732700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222732700">(Jan 14 2021 at 15:29)</a>:</h4>
<p>(deleted)</p>



<a name="222732803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222732803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222732803">(Jan 14 2021 at 15:29)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="n">stdx</span>::<span class="n">assert_never</span><span class="o">!</span><span class="p">(</span><span class="o">!</span><span class="n">cond</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">handle_impossible</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="222733015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222733015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222733015">(Jan 14 2021 at 15:31)</a>:</h4>
<p>I like it, it splits the difference between "this should never happen and I want to check that" and "but it'd actually be pretty easy to do something sensible in this situation", which is a dilemma that I have all the time...</p>



<a name="222733183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222733183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222733183">(Jan 14 2021 at 15:32)</a>:</h4>
<p>yay fearless assertivness!</p>



<a name="222768565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222768565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222768565">(Jan 14 2021 at 19:20)</a>:</h4>
<p>I think you might've forgotten to push the <code>assert_always</code> macro with it, the <code>assert_never</code> macro references it in the first transcriber but it doesn't exist <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> <a href="https://github.com/rust-analyzer/rust-analyzer/commit/8dc68ecdfcc764c7c0dcf5fcedcb51b092d99620#diff-4ab2b49eae91be2ce0307d9a427fb0cacf505aec4881b742d9bee842d580e81bR69">https://github.com/rust-analyzer/rust-analyzer/commit/8dc68ecdfcc764c7c0dcf5fcedcb51b092d99620#diff-4ab2b49eae91be2ce0307d9a427fb0cacf505aec4881b742d9bee842d580e81bR69</a></p>



<a name="222772826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222772826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222772826">(Jan 14 2021 at 19:46)</a>:</h4>
<p>argh</p>



<a name="222772925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222772925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222772925">(Jan 14 2021 at 19:47)</a>:</h4>
<p>it's rather that I was lazy to push implement both macros, and botched a rename from always to never</p>



<a name="222773089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/222773089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#222773089">(Jan 14 2021 at 19:48)</a>:</h4>
<p>noting defeats the advantages of a strong static type system quite as effectively as a powerful macro machinery</p>



<a name="223482413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/223482413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#223482413">(Jan 21 2021 at 08:53)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="232957">@Jack Huey</span>, I think long-term it might make sense to use something like this for chalk? We hit assertion errors fairly often, and, long term, they shouldn't crash the whole request. </p>
<p>OTOH, right now crashing rust-analyzer might be nice, because it forces users to come up with sweat minimal examples: <a href="https://github.com/rust-analyzer/rust-analyzer/issues/6956#issuecomment-764459620">https://github.com/rust-analyzer/rust-analyzer/issues/6956#issuecomment-764459620</a></p>



<a name="223490087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/223490087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#223490087">(Jan 21 2021 at 10:20)</a>:</h4>
<p>I don't know, e.g. the "wrong number of type parameters substituted into a type" indicates a rather severe bug somewhere, and at the point of that assertion it's a pretty deep assumption that's very hard to continue without</p>



<a name="223491270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/223491270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#223491270">(Jan 21 2021 at 10:34)</a>:</h4>
<p>That's also true. Hm, don't we wrap chalk into catch_unwind already? I guess then it doesn't really matter</p>



<a name="223491828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/223491828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#223491828">(Jan 21 2021 at 10:40)</a>:</h4>
<p>I don't think we do anymore</p>



<a name="223491868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/223491868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#223491868">(Jan 21 2021 at 10:41)</a>:</h4>
<p>I guess we could handle it similar to an assert_never around the whole solver call</p>



<a name="223514901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20always%20assert%20never/near/223514901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20always.20assert.20never.html#223514901">(Jan 21 2021 at 14:28)</a>:</h4>
<p>I'm with <span class="user-mention" data-user-id="129457">@Florian Diebold</span> here: it would be really hard to recover from most of the errors we encounter. It might be worth introducing some kind of <code>FatalError</code> state that can be returned instead of a solution (or lack of). Then try to bubble that up. But that would mean refactoring basically the entire codebase. At that point, it's probably easier to just panic/catch_unwind</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>